﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Security.Principal;

namespace Db8.Data
{
    [Table("Users")]
    public class User : IPrincipal
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long UserId { get; set; }

        [Required]
        [StringLength(50, ErrorMessage = "Username must be 50 characters or fewer")]
        [Display(Name = "Username")]
        public string Username { get; set; }

        [Required]
        public DateTime LastActivityDt { get; set; }

        [Required]
        public DateTime LastUpdatedDt { get; set; }

        [Required]
        [StringLength(320, ErrorMessage = "Email must be 320 characters or fewer")]
        public string Email { get; set; }

        [Required]
        [StringLength(50, ErrorMessage = "Name must be 50 characters or fewer")]
        [Display(Name = "Name")]
        public string Name { get; set; }

        [StringLength(50, ErrorMessage = "First Name must be 50 characters or fewer")]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [StringLength(50, ErrorMessage = "Middle Name must be 50 characters or fewer")]
        [Display(Name = "Middle Name")]
        public string MiddleName { get; set; }

        [StringLength(50, ErrorMessage = "Last Name must be 50 characters or fewer")]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        // http://graph.facebook.com/[id]/picture
        public string Picture
        {
            get
            {
                if (UserId == 999999999 && Name == "Test User")
                    return "http://graph.facebook.com/barney.oran/picture";
                return "http://graph.facebook.com/" + UserId + "/picture";
            }
        }

        public string Link
        {
            get
            {
                if (UserId == 999999999 && Name == "Test User")
                    return "http://www.facebook.com/barney.oran";
                return "http://www.facebook.com/" + UserId;
            }
        }

        public bool IsSystemAdministrator
        {
            get
            {
                return UserRoles.Any(ur => ur.Role.Name == Db8Roles.SYSTEM_ADMINISTRATOR);
            }
        }

        public bool IsContentAdministrator
        {
            get
            {
                return UserRoles.Any(ur => ur.Role.Name == Db8Roles.CONTENT_ADMINISTRATOR);
            }
        }


        [Required]
        public int Score { get; set; }

        [StringLength(500, ErrorMessage = "AccessToken is limited to 500 characters")]
        public string AccessToken { get; set; }

        public virtual ICollection<UserRole> UserRoles { get; set; }


        // user activity
        public virtual ICollection<Claim> LastModifiedClaims { get; set; }
        public virtual ICollection<Claim> CreatedClaims { get; set; }
        //public virtual ICollection<UserActivity> UserActivities { get; set; }


        #region IPrincipal

        public IIdentity Identity
        {
            get
            {
                return new Db8Identity() { Name = Username };
            }
        }

        public bool IsInRole(string role)
        {
            if (UserRoles.Any(ur => ur.Role.Name == role))
                return true;
            return false;
        }

        #endregion
    }

    public class Db8Identity : IIdentity
    {
        public string AuthenticationType { get { return "Forms"; } }

        public bool IsAuthenticated { get { return true; } }

        public string Name { get; set; }
    }
}
